package com.gzdh.dishrecognition.config;

import android.os.Environment;
import android.util.Log;

import com.gzdh.dishrecognition.util.FileUtils;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;

/**
 * author : shangrong
 * date : 2019/5/23 11:23 AM
 * description :配置BaseConfig单例
 */
public class MyBaseConfig {
    public static final String folder = Environment.getExternalStorageDirectory() + File.separator + "com.chelmey.android.dishidf";
    private static final String filePath = folder + File.separator + "Config.txt";
    private static final String TAG = "MyBaseConfig";

    private int libraryId = 1;

    private static MyBaseConfig myBaseConfig;

    private MyBaseConfig() {

    }

    public static MyBaseConfig getMyBaseConfig() {
        if (myBaseConfig == null) {
            myBaseConfig = new MyBaseConfig();
        }
        return myBaseConfig;
    }

    public int getLibraryId() {
        return libraryId;
    }

    public void setLibraryId(int libraryId) {
        this.libraryId = libraryId;
    }

    public static boolean isConfigExit() {
        File file1 = new File(folder);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        File file = new File(filePath);
        if (file.exists()) {
            return true;
        } else {
            try {
                file.createNewFile();
                modityJson();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }

    /**
     * 修改配置文件内容并重新读取配置
     */
    public static boolean modityJson() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("libraryId", MyBaseConfig.getMyBaseConfig().getLibraryId());
            // 修改内容写入配置文件
            FileUtils.writeTxtFile(jsonObject.toString(), filePath);
            // 重新读取配置文件内容
            initConfig();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 读取配置文件内容
     *
     * @return
     */
    public static Boolean initConfig() {
        String configMessage = FileUtils.txt2String(filePath);
        if (configMessage.equals("")) {
            Log.e(TAG, "文件不存在");
            return false;
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(configMessage);
            if (!identify(jsonObject)) {
                return false;
            }
            System.out.println(jsonObject.toString());
            MyBaseConfig.getMyBaseConfig().setLibraryId(jsonObject.getInt("libraryId"));

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("facesdk", "文件内容异常,请检测是否规范");
            return false;
        }
    }

    public static boolean identify(JSONObject jsonObject) {
        try {
            int libraryId = (int) jsonObject.get("libraryId");
            if (libraryId < 0) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "文件内容格式异常,请检测是否规范");
            return false;
        }
        return true;
    }
}
